package com.cqust.wdm.emsboot.controller;


import com.cqust.wdm.emsboot.config.security.ShiroUtils;
import com.cqust.wdm.emsboot.domain.Movement;
import com.cqust.wdm.emsboot.domain.User;
import com.cqust.wdm.emsboot.service.IMovementService;
import com.cqust.wdm.emsboot.util.AjaxResult;
import com.cqust.wdm.emsboot.util.BaseController;
import com.cqust.wdm.emsboot.util.ExcelUtil;
import com.cqust.wdm.emsboot.util.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单信息Controller
 */
@RestController
@RequestMapping("/emsboot/movement")
public class MovementController extends BaseController {
    @Autowired
    private IMovementService movementService;

    /**
     * 查询订单信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Movement movement) {
        User userInfo = ShiroUtils.getUserInfo();

        if (userInfo != null) {
            //普通用户查询 自己的入库单，出库单
            if ("普通用户".equals(userInfo.getRoleName()))
                movement.setUid(Long.valueOf(userInfo.getId()));
        }
        startPage();
        List<Movement> list = movementService.selectMovementList(movement);
        return getDataTable(list);
    }



    /**
     * 查询订单信息列表
     */
    @GetMapping("/export")
    public AjaxResult export(Movement movement) {
        startPage();
        List<Movement> list = movementService.selectMovementList(movement);
        ExcelUtil<Movement> util = new ExcelUtil<Movement>(Movement.class);
        return util.exportExcel(list, "订单数据");
    }



    /**
     * 获取订单信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(movementService.selectMovementById(id));
    }

    /**
     * 新增订单信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody Movement movement) {

        return toAjax(movementService.insertMovement(movement));
    }

    /**
     * 修改订单信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Movement movement) {
        return toAjax(movementService.updateMovement(movement));
    }

    /**
     * 删除订单信息
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(movementService.deleteMovementByIds(ids));
    }
}
